package com.thinkingstar.iads.datacenter.entity.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.formula.functions.T;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Criteria;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;
import org.nutz.trans.Atom;
import org.nutz.trans.Trans;

import com.thinkingstar.iads.common.serviceImpl.EntityRepositoryServiceImpl;
import com.thinkingstar.iads.common.utils.SwapUtil;
import com.thinkingstar.iads.datacenter.entity.DcDesignContentMajor;
import com.thinkingstar.iads.datacenter.entity.DcProjectMentionInformation;
import com.thinkingstar.iads.datacenter.entity.DcProjectMentionInformationContentMajor;

/**
 * @ClassName: DaoDcProjectDesignContentMajor 
 * @Description:数据中心_项目_项目设计内容与相关专业关联维护
 * @author 1157372219@qq.com 
 * @date 2018年3月19日   
 */
@IocBean(args = {"refer:dao"})
public class DaoDcProjectDesignContentMajor extends EntityRepositoryServiceImpl<DcDesignContentMajor>{

	public DaoDcProjectDesignContentMajor(Dao dao) {
		super(dao);
	}
	
	/**
	 * 逻辑删除
	 * @param id
	 * @return
	 * @throws Exception 
	 */
	public int delDesignContentMajor(String id) throws Exception{
		return delObj(id);
	}
	
	/**
	 * 添加设计内容与专业的关联关系并绑定提入资料信息
	 */
	public void addProjectDesignMajorMention(final List <DcProjectMentionInformation> mentionInformations , String design_id , String major_id , String design_phase_id){
		Trans.exec(new Atom(){
		    public void run() {
				for(DcProjectMentionInformation mentionInformation : mentionInformations){
					addProjectDesignMajorMention(mentionInformation.getId(),major_id,design_id,design_phase_id);
				}
		    }
		});
	}
	
	/**
	 * 添加提入资料与设计内容与专业的绑定
	 * @param mention_id
	 * @param design_id
	 * @param major_id
	 */
	public void addProjectDesignMajorMention(String mention_id , String major_id , String content_id , String design_phase_id){
		DcProjectMentionInformationContentMajor dc = new DcProjectMentionInformationContentMajor();
		dc.setProject_design_phase_id(design_phase_id);
		dc.setProject_mention_information_id(mention_id);
		dc.setContent_id(content_id);
		dc.setMajor_id(major_id);
		dao().insert(dc);
	}
	
	
	
	/**
	 * 添加设计内容与专业的关联记录
	 * @param design_id
	 * @param major_id
	 */
	public DcDesignContentMajor addProjectDesignMajor(String design_id , String major_id){
		DcDesignContentMajor dcDesignContentMajor = new DcDesignContentMajor();
		dcDesignContentMajor.setProject_design_content_id(design_id);
		dcDesignContentMajor.setProject_design_major_id(major_id);
		return dao().insert(dcDesignContentMajor);
	}
	
	/**
	 * 清空与指定设计内容有关的关联记录
	 */
	public void delDesignContentMajorByContentId(String content_id){
		String del_sql = " delete from dc_design_content_major "
				       + " where project_design_content_id = @content_id ";
		Sql sql = Sqls.create(del_sql);
		sql.params().set("content_id", content_id);
		dao().execute(sql);
	}
	
	/**
	 * 根据content_id查询已匹配以及未匹配专业的设计内容等
	 * @return
	 */
	public List<Map> findMatchingMajorByContentId(String content_id){
		List <Map> list = new ArrayList<>();
		String find_sql = " select ddm.id major_id , ddm.project_design_major_name major_name ,"
				        + " ifnull((select dcm.project_design_content_id"
				        + " from dc_design_content_major dcm where dcm.del_flag = @dcm_del_flag "
				        + " and dcm.project_design_content_id = @dcm_content_id and dcm.project_design_major_id = ddm.id ),'') content_id "
				        + " from dc_project_design_major ddm "
				        + " where ddm.del_flag = @ddm_del_flag ";
		Sql sql = Sqls.create(find_sql);
		sql.params().set("dcm_del_flag",false).set("dcm_content_id", content_id).set("ddm_del_flag", false);
		sql.setCallback(new SqlCallback() {
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				List <Map> result_list = new LinkedList<>();
				while(rs.next()){
					Map <String , Object> map = new HashMap<>();
					map.put("major_id", rs.getString("major_id"));
					map.put("major_name", rs.getString("major_name"));
					map.put("content_id", rs.getString("content_id"));
					result_list.add(map);
				}
				SwapUtil.clearNull(result_list);
				return result_list;
			}
		});
		dao().execute(sql);
		list = sql.getList(Map.class);
		return list;
	}
	
	//根据设计内容id查询已关联的专业
	public List <NutMap> findCorrelationMajorByContentId(String content_strs){
		List <NutMap> list = new ArrayList<>();
		StringBuffer find_sql = new StringBuffer(" select distinct dpdm.id major_id , dpdm.project_design_major_name major_name , dpdm.project_design_major_code major_code ,"
				+ " dpdm.serial_number serial_number"
				+ " from dc_design_content_major ddcm inner join dc_project_design_major dpdm on ddcm.project_design_major_id = dpdm.id"
				+ " and dpdm.project_design_major_name not like '%动车设施%' "
				+ " where ddcm.del_flag = '0' and dpdm.del_flag = '0' and ddcm.project_design_content_id in"+content_strs
				+ " order by dpdm.serial_number+0 asc");
		Sql sql = Sqls.create(find_sql.toString());
		sql.setCallback(new SqlCallback() {
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				List <NutMap> list = new ArrayList<>();
				while (rs.next()) {
					NutMap map = new NutMap();
					map.put("major_id", rs.getString("major_id"));
					map.put("major_name", rs.getString("major_name"));
					map.put("major_code", rs.getString("major_code"));
					list.add(map);
				}
				return list;
			}
		});
		dao().execute(sql);
		return sql.getList(NutMap.class);
	}
	
	

}
